/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nuatar.meibi.tool;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author AnchLi
 */
public class PowerTool {
    private static String skey="71de60464d20cd501febd2a1f9e619f6";
    private static String Outkey="";
    public static String getKey(String bucket,String Method,String Object,Long size){
        String result="";
        StringBuilder localStringBuilder1 = new StringBuilder();
        StringBuilder localStringBuilder2 = new StringBuilder();
        localStringBuilder1.append("MBO");
        localStringBuilder2.append("Method=").append(Method).append("\n");
        localStringBuilder2.append("Bucket=").append(bucket).append("\n");
        localStringBuilder2.append("Object=").append(Object).append("\n");
         if (size!=null&&size!= 0) {
        localStringBuilder1.append("S");
        localStringBuilder2.append("Size=").append(size).append("\n");
       
      }
        localStringBuilder2.insert(0, "\n");
        localStringBuilder2.insert(0, localStringBuilder1.toString());
        byte[] arrayOfByte = new byte[0];
        SecretKeySpec localSecretKeySpec;
        localSecretKeySpec = new SecretKeySpec(skey.getBytes(), SigningAlgorithm.HmacSHA1.toString());  
        try {
             Mac localMac = Mac.getInstance(localSecretKeySpec.getAlgorithm());
            localMac.init(localSecretKeySpec);
            arrayOfByte = localMac.doFinal(ServiceUtils.toByteArray(localStringBuilder2.toString()));
           result=toBase64(arrayOfByte);
        } catch (InvalidKeyException ex) {
          Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    
    }
    public static String getOutKey(String key,String bucket,String Method,String Object,Long size){
        Outkey=key;
        String result="";
        StringBuilder localStringBuilder1 = new StringBuilder();
        StringBuilder localStringBuilder2 = new StringBuilder();
        localStringBuilder1.append("MBO");
        localStringBuilder2.append("Method=").append(Method).append("\n");
        localStringBuilder2.append("Bucket=").append(bucket).append("\n");
        localStringBuilder2.append("Object=").append(Object).append("\n");
         if (size!= null) {
        localStringBuilder1.append("S");
        localStringBuilder2.append("Size=").append(size).append("\n");
       
      }
        localStringBuilder2.insert(0, "\n");
        localStringBuilder2.insert(0, localStringBuilder1.toString());
        byte[] arrayOfByte = new byte[0];
        SecretKeySpec localSecretKeySpec;
        localSecretKeySpec = new SecretKeySpec(Outkey.getBytes(), SigningAlgorithm.HmacSHA1.toString());  
        try {
             Mac localMac = Mac.getInstance(localSecretKeySpec.getAlgorithm());
            localMac.init(localSecretKeySpec);
            arrayOfByte = localMac.doFinal(ServiceUtils.toByteArray(localStringBuilder2.toString()));
           result=toBase64(arrayOfByte);
        } catch (InvalidKeyException ex) {
          Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PowerTool.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    
    }
    public static String toBase64(byte[] paramArrayOfByte) throws UnsupportedEncodingException {
      
        return URLEncoder.encode(new String(Base64.encodeToChar(paramArrayOfByte,false)), "UTF-8") ;
  }
}
